﻿// Decompiled with JetBrains decompiler
// Type: LayersParser.Objects.Process
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using LayersParser.Extensions;
using LayersParser.Interfaces;
using System.Collections.Generic;
using System.Xml.Linq;


namespace LayersParser.Objects;

public class Process : Command, ICommand
{
  private string _name;
  private List<Motion> motions = new List<Motion>();
  private List<Param> _relatedParams = new List<Param>();
  private Dialog _dialog = new Dialog(new List<string>());
  private string location;

  public Process()
  {
    this.Node = new XElement(new XElement((XName) nameof (Process), new object[5]
    {
      (object) new XAttribute((XName) "Type", (object) ""),
      (object) new XElement((XName) "Force"),
      (object) this._dialog.Node,
      (object) new XElement((XName) "UILayer"),
      (object) new XElement((XName) "SimulationLayer")
    }));
    this._dialog.Node = this.Node.Element((XName) nameof (Dialog));
  }

  public Process(XElement processNode, List<Param> parameters, string locationtype)
  {
    this.location = locationtype;
    this.Node = processNode;
    this.Name = (string) processNode?.Attribute((XName) "Type");
    this._dialog = new Dialog(processNode?.Element((XName) nameof (Dialog)));
    foreach (XElement element in processNode?.Element((XName) nameof (Dialog))?.Element((XName) "RoboticParamRef")?.Elements((XName) "Param"))
    {
      if (element.Value != null && element.Value != "")
      {
        Param obj = parameters.ReturningParameter(element.Value);
        obj.Command = true;
        this._relatedParams.Add(obj);
      }
      else if (element.Attribute((XName) nameof (Name)) != null)
      {
        Param obj = parameters.ReturningParameter((string) element.Attribute((XName) nameof (Name)));
        obj.Command = true;
        this._relatedParams.Add(obj);
      }
    }
    if (processNode.Element((XName) "Force") == null)
      processNode.Add((object) new XElement((XName) "Force"));
    foreach (XElement element in processNode.Element((XName) "Force")?.Elements((XName) "Param"))
    {
      Param newParam = this._relatedParams.ReturningParameter(element.Attribute((XName) nameof (Name)).ToString());
      newParam.NewValue = element.Attribute((XName) "NewValue").ToString();
      this._relatedParams.ReplaceParameter(newParam);
    }
    foreach (XElement element in processNode?.Elements((XName) "Motion"))
      this.motions.Add(new Motion(processNode, int.Parse(element.Attribute((XName) "Type").Value)));
  }

  public override Dialog Dialog
  {
    get => this._dialog;
    set => this._dialog = value;
  }

  public override List<Param> Params
  {
    get => this._relatedParams;
    set
    {
      this._relatedParams = value;
      XDocument document1 = this.Node.Document;
      if (document1 != null)
      {
        XElement roboticParamRefProc = document1.GoToRoboticParamRefProc(this.Name, this.location);
        if (roboticParamRefProc != null)
        {
          IEnumerable<XElement> source = roboticParamRefProc.Elements((XName) "Param");
          if (source != null)
            source.Remove<XElement>();
        }
      }
      foreach (Param relatedParam in this._relatedParams)
      {
        XDocument document2 = this.Node.Document;
        if (document2 != null)
          document2.GoToRoboticParamRefProc(this.Name, this.location)?.Add((object) new XElement((XName) "Param", (object) relatedParam.Name));
        if (relatedParam.Node.Parent == null)
        {
          XDocument document3 = this.Node.Document;
          if (document3 != null)
            document3.GoToRoboticParams()?.Add((object) relatedParam.Node);
        }
      }
      foreach (Motion motion in this.motions)
        motion.Params = this._relatedParams.ParToStr();
    }
  }

  public override string Name
  {
    get => this._name;
    set
    {
      this._name = value;
      if (this.Node == null)
        return;
      if (this.Node.Attribute((XName) "Type") != null)
        this.Node.Attribute((XName) "Type").Value = this._name;
      this._dialog.Title = this._name;
    }
  }

  public List<Motion> Motions
  {
    get => this.motions;
    set
    {
      this.motions = value;
      this.Node.Elements((XName) "Motion").Remove<XElement>();
      foreach (Motion motion in this.motions)
        this.Node.Add((object) new XElement((XName) "Motion", new object[2]
        {
          (object) new XAttribute((XName) "Type", (object) motion.MotionType.ToString()),
          (object) motion.DownloadLayer.Node
        }));
    }
  }

  public string Location
  {
    get => this.location;
    set => this.location = value;
  }
}
